package com.power.dao.impl;

import com.power.dao.LineDao;
import com.power.ov.ResultDataTable;
import com.power.pojo.Line;
import com.power.pojo.Users;
import com.power.utils.JdbcBuild;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/07 17:04
 * Description:
 * Version: V1.0
 */
public class LineDaoimpl extends JdbcBuild implements LineDao {

    @Override
    public ResultDataTable QueryListLine(int page, int count) throws SQLException {
//        String sql = "SELECT line_id,line_number,line_name,start_tower,stop_tower,tower_number,st.state_name2,s.state_name  FROM line AS l,states AS s,state AS st WHERE l.line_stae=s.state_id AND st.state_id2=l.line_run limit ?,?";
        String sql = "SELECT *  FROM line AS l,states AS s,state AS st WHERE l.line_stae=s.state_id AND st.state_id2=l.line_run limit ?,?";
        int param1 = (page-1)*count;
        int param2 = count;
        Object[] params = {param1,param2};
        ResultDataTable resultDataTable = new ResultDataTable();
        List<Line> lineList = super.queryListEntity(Line.class,sql,params);
        resultDataTable.setCode(0);
        resultDataTable.setData(lineList);
        resultDataTable.setCount(QueryLineCount());
        return resultDataTable;
    } private long QueryLineCount() throws SQLException {
        String sql ="SELECT count(*)  FROM line AS l,states AS s,state AS st WHERE l.line_stae=s.state_id AND st.state_id2=l.line_run";
        long result = super.findAllForInt(sql,null);
        return result;
    }

    @Override
    public int ModifiesLineStatus(int newstate, int line_id) throws SQLException {
        String sql = "update line set line_stae = ?  where line_id = ?";
        Object[] params = {newstate,line_id};
        return super.executeUpdate(sql,params);
    }

    /**
     * 根据line_id更改tower中的启用状态
     * @param newstate
     * @param line_id
     * @return
     * @throws SQLException
     */
    @Override
    public int ModifiesTowerStatus(int newstate, int line_id) throws SQLException {
        String sql = "update tower set tower_state = ? where line_id = ?";
        Object[] params = {newstate,line_id};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int DeletLine(String line_id) throws SQLException {
        String sql="delete from line where line_id = ?";

        return super.executeUpdate(sql,line_id);
    }

    @Override
    public int ModifiesLineDate(Line line) throws SQLException {
        String sql="UPDATE line SET line_number=?,line_name=?,line_length=?,line_backlength=?,line_date=?,line_rank=?,start_tower=?,stop_tower=?,tower_number=?,line_notes=?,line_stae=? WHERE line_id=?;";
        Object[] params={line.getLine_number(),line.getLine_name(),line.getLine_length(),line.getLine_backlength(),line.getLine_date(),line.getLine_rank(),line.getStart_tower(),line.getStop_tower(),line.getTower_number(),line.getLine_notes(),line.getLine_stae(),line.getLine_id()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public int AddLine(Line line) throws SQLException {
        String sql = "INSERT INTO line(line_number,line_name,line_length,line_backlength,line_date,line_rank,start_tower,stop_tower,tower_number,line_notes,line_stae) VALUES (?,?,?,?,?,?,?,?,?,?,?) ";
        Object[] params={line.getLine_number(),line.getLine_name(),line.getLine_length(),line.getLine_backlength(),line.getLine_date(),line.getLine_rank(),line.getStart_tower(),
        line.getStop_tower(),line.getTower_number(),line.getLine_notes(),line.getLine_stae()};
        int result = super.executeUpdate(sql,params);
        return result;
    }

    @Override
    public ResultDataTable SearchLine(String line_number, int line_run, int page, int limit) throws SQLException {
        String sql = "SELECT *  FROM line AS l,states AS s,state AS st WHERE l.line_stae=s.state_id AND st.state_id2=l.line_run and line_number=? AND line_run=? limit ?,?";
        int param1 = (page-1)*limit;
        int param2 = limit;
        Object[] params = {line_number,line_run,param1,param2};
        List<Line> lineList =super.queryListEntity(Line.class,sql,params);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(lineList);
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功");
        resultDataTable.setCount(queryCountLine(line_number,line_run));
        System.out.println("进入到dao层，获得数据信息为"+resultDataTable.toString());
        return resultDataTable;
    }

    private long queryCountLine(String line_number, int line_run) throws SQLException {
        String sql = "SELECT count(*) FROM line WHERE line_number=? AND line_run=? ";
        Object[] params = {line_number,line_run};
        long result = super.findAllForInt(sql,params);
        return result;
    }

    @Override
    public ResultDataTable ShowSelectLine() throws SQLException {
        String sql = "SELECT * FROM line";
        List<Line> lineList = super.queryListEntity(Line.class, sql, null);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(lineList);
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功！！！");
        return resultDataTable;
    }

    @Override
    public ResultDataTable showUser() throws SQLException {
        String sql ="SELECT * FROM users WHERE role_id=3";
        List<Users> users = super.queryListEntity(Users.class, sql, null);
        ResultDataTable  resultDataTable = new ResultDataTable();
        resultDataTable.setData(users);
        resultDataTable.setMessage("查询成功");
        resultDataTable.setCode(0);
        return resultDataTable;
    }

}



































